package top.kaoshanji.p1ch03a;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

/**
 * author kaoshanji
 * ^_^
 * create time 2025/2/10 17:53
 */
public class Sorting {

    private static final Logger logger = LogManager.getLogger(Sorting.class);

    public static void main(String[] args) {

        List<Apple> inventory = new ArrayList<>();
        inventory.addAll(Arrays.asList(
                new Apple(80, Color.GREEN),
                new Apple(155, Color.GREEN),
                new Apple(120, Color.RED)
        ));

        // 代码清单-p1ch03a-62页a：传递代码
        inventory.sort(new AppleComparator());
        logger.info("...传递代码：{}...", inventory);

        inventory.set(1, new Apple(30, Color.GREEN));


        // 代码清单-p1ch03a-62页b：使用匿名类
        inventory.sort(new Comparator<Apple>() {
            @Override
            public int compare(Apple o1, Apple o2) {
                return o1.getWeight() - o2.getWeight();
            }
        });
        logger.info("...使用匿名类：{}...", inventory);

        inventory.set(1, new Apple(20, Color.RED));


        // 代码清单-p1ch03a-63页a：使用Lambda表达式
        inventory.sort((a1, a2) -> a1.getWeight() - a2.getWeight());
        logger.info("...使用Lambda表达式：{}...", inventory);

        inventory.set(1, new Apple(10, Color.RED));


        // 代码清单-p1ch03a-63页b：使用方法引用
        inventory.sort(Comparator.comparing(Apple::getWeight));
        logger.info("...使用方法引用：{}...", inventory);



    }

}
